home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11383 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: char/int conversion and comparison
  5. Date: 23 Mar 1996 17:36:32 GMT
  6. Organization: Borland International
  7. Message-ID: <4j1cr0$dpc@druid.borland.com>
  8. References: <4j0234$7hi@nuscc.nus.sg>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4j0234$7hi@nuscc.nus.sg>, eng30403@leonis.nus.sg says...
  15. >
  16. >        I got confused by the conversion and comparison between int and 
  17. >char. Consider the following fragment of code :
  18. >
  19. >        char foo1='a';    /* 1st line */
  20. >        int foo2='a';     /* 2nd line */
  21. >
  22. >        if (foo2==foo1);  /* 3rd line */
  23. >
  24. >        Is this comparison guaranteed to evaluate to TRUE on all machines ?
  25. >
  26. >        I have read that on different machines, char may be implemented 
  27. >as signed or unsigned. Hence if sizeof(int) > sizeof(char), a promotion 
  28. >of char to int may or may not extend the most significant bit.
  29. >
  30.  
  31. Correct. However, all characters in the source character set 
  32. (A-Z,a-z,0-9,!=#%&'()*+,-./:;<=>?[\]^_{|}~) are guaranteed to be positive. So 
  33. you don't have to worry about sign extension affecting 'a'. You do have to 
  34. worry about it for something like '\xff'.
  35.  
  36. >        On the other hand, a conversion of int to char, (if sizeof(int) > 
  37. >sizeof(char) ) results in the more significant bits being truncated. In 
  38. >line 1 above, the int 'a' is truncated and converted to a char foo1. 
  39. >Subsequently, the char foo1 is promoted to an int in the comparison with 
  40. >foo2.
  41. >
  42.  
  43. Yup. But this shouldn't affect the value. That is,
  44.  
  45. char ch = X;
  46. int i = ch;
  47. char ch1 = i;
  48.  
  49. ch1 will compare equal to ch.
  50.  
  51. >        Since we cant tell whether the sign bit is extended or not, is it 
  52. >wise to use such a comparison if we want to be portable ?
  53.  
  54. The problem, as I indicated earlier, arises when you go outside the source 
  55. character set, and then only if you perform some operation on the character 
  56. that you are dealing with. You may be surprised by the result if you don't pay 
  57. enough attention to sign extension.
  58.     Incidentally, you can tell whether char is signed or unsigned, which in 
  59. turn tells you whether promotions to int will result in sign extension. The 
  60. header limits.h defines the macros SCHAR_MIN, SCHAR_MAX, UCHAR_MIN, UCHAR_MAX, 
  61. CHAR_MIN, and CHAR_MAX. CHAR_MIN and CHAR_MAX will hold values that are 
  62. identical to the values of SCHAR_MIN and SCHAR_MAX if chars are signed, or 
  63. values that are identical to the values of UCHAR_MIN and UCHAR_MAX if chars are 
  64. unsigned.
  65.     -- Pete
  66.  
  67.